package com.demo.main.dao.impl;

import com.demo.main.dao.BorrowRecordDao;
import com.demo.main.entity.BorrowRecord;
import com.demo.main.vo.BorrowRecordVo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// BorrowRecordDaoImpl类，实现了BorrowRecordDao接口，并继承了BaseDaoImpl类，泛型参数为BorrowRecord和Integer
public class BorrowRecordDaoImpl extends BaseDaoImpl<BorrowRecord, Integer> implements BorrowRecordDao {
    // 定义SQL查询语句，用于查询借阅记录，包括书籍和用户信息
    private final String borrowRecordSql = "select borrow_id, borrow_record.user_id, borrow_record.book_id, borrow_date, due_date, return_date, `user`.username, book.title as book_title from borrow_record, book, `user` where book.book_id = borrow_record.book_id and `user`.user_id = borrow_record.user_id";

    // 构造方法，调用父类的构造方法，初始化表名、主键字段名和实体类
    public BorrowRecordDaoImpl() {
        super("borrow_record", "borrow_id", BorrowRecord.class);
    }
    // 根据当前用户ID和页码，查询借阅记录值对象列表
    @Override
    public List<BorrowRecordVo> selectBorrowRecordVoByCurrentUser(Integer userId, Integer pageNum) {
        // 创建条件Map，设置用户ID条件
        Map<String, Object> conditions = new HashMap<>();
        conditions.put("borrow_record.user_id", userId);
        // 调用父类的selectByConditionWithDetailsDesc方法，根据条件、SQL语句、值对象类和页码查询借阅记录
        return selectByConditionWithDetailsDesc(borrowRecordSql, conditions, BorrowRecordVo.class, pageNum);
    }
    // 根据用户ID查询符合条件的借阅记录总数
    @Override
    public int selectBorrowRecordCountByCondition(Integer userId) {
        // 创建条件Map，设置用户ID条件
        Map<String, Object> conditions = new HashMap<>();
        conditions.put("user_id", userId);
        // 调用父类的selectCountByConditionWithDetails方法，根据条件和SQL语句查询借阅记录总数
        return selectCountByConditionWithDetails(borrowRecordSql, conditions);
    }
    // 根据页码查询所有借阅记录值对象列表
    @Override
    public List<BorrowRecordVo> selectAllBorrowRecordVo(Integer pageNum) {
        // 调用父类的selectAllWithDetailsDesc方法，根据SQL语句、值对象类和页码查询所有借阅记录
        return selectAllWithDetailsDesc(borrowRecordSql, BorrowRecordVo.class, pageNum);
    }
}
